package cn.school.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;

import cn.school.dto.*;
import cn.school.mapper.PaperSubmissionMapper;
import cn.school.mapper.StudentsMapper;
import cn.school.mapper.TeacherMapper;
import cn.school.pojo.PaperSubmission;
import cn.school.pojo.Students;
import cn.school.pojo.Teacher;
import cn.school.service.PaperSubmissionService;
import cn.school.vo.FindPaperSubmissionListVo;
import cn.school.vo.FindPaperSubmissionVo;
import cn.school.vo.ResultVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
* @author Lenovo
* @description 针对表【paper_submission(试卷提交表)】的数据库操作Service实现
* @createDate 2025-06-10 20:03:02
*/
@Service
public class PaperSubmissionServiceImpl extends ServiceImpl<PaperSubmissionMapper, PaperSubmission>
    implements PaperSubmissionService {
    @Resource
    private PaperSubmissionMapper paperSubmissionMapper;
    @Resource
    private StudentsMapper studentsMapper;
    @Resource
    private TeacherMapper teacherMapper;
    @Override
    public String addPaperSubmission(PaperSubmissionDto paperSubmissionDto, HttpServletRequest request) {
        String token = request.getHeader("token");
        JWT jwt = JWTUtil.parseToken(token);
        Object id = jwt.getPayload("id");
        QueryWrapper<Students> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",id);
        Students students = studentsMapper.selectOne(queryWrapper);
        if(students==null){
            return "您尚未成为本校学生没有权限提交试卷";
        }else {
            PaperSubmission paperSubmission = new PaperSubmission();
            paperSubmission.setId(IdUtil.getSnowflake(1,1).nextId());
            String examPaperId = paperSubmissionDto.getExamPaperId();
            paperSubmission.setExamPaperId(Long.valueOf(examPaperId));
            paperSubmission.setStudentId(Long.valueOf(students.getId()));
            String questionId = paperSubmissionDto.getQuestionId();
            paperSubmission.setQuestionId(Integer.valueOf(questionId));
            paperSubmission.setImageUrl(paperSubmissionDto.getImageUrl());
            paperSubmission.setScore(Double.valueOf(0));
            paperSubmission.setIsDel(0);
            int result = paperSubmissionMapper.insert(paperSubmission);
            if(result==1){
                return "提交成功";
            }else {
                return "提交失败";
            }
        }
    }

    @Override
    public String setScore(PaperSetScoreDto paperSetScoreDto, HttpServletRequest request) {
        String token = request.getHeader("token");
        JWT jwt = JWTUtil.parseToken(token);
        Object id = jwt.getPayload("id");

        QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();
        teacherQueryWrapper.eq("user_id",id);
        Teacher teacher = teacherMapper.selectOne(teacherQueryWrapper);
        if(teacher==null){
            return "您没有权限设置成绩";
        }else {
            QueryWrapper<PaperSubmission> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id",paperSetScoreDto.getPaperSubmissionId());
            PaperSubmission paperSubmission = paperSubmissionMapper.selectOne(queryWrapper);
            paperSubmission.setScore(paperSetScoreDto.getScore());
            paperSubmission.setTeacherId(teacher.getId());
            int result = paperSubmissionMapper.updateById(paperSubmission);
            if(result>0){
                return "提交成绩成功";
            }
            else {
                return "提交成绩失败";
            }
        }
    }

    @Override
    public String delScore(DelScoreDto delScoreDto, HttpServletRequest request) {
        String token = request.getHeader("token");
        JWT jwt = JWTUtil.parseToken(token);
        Object id = jwt.getPayload("id");
        QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();
        teacherQueryWrapper.eq("user_id",id);
        Teacher teacher = teacherMapper.selectOne(teacherQueryWrapper);
        if(teacher==null){
            return "您没有权限删除成绩";
        }
        else {
            QueryWrapper<PaperSubmission> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("exam_paper_id",delScoreDto.getExamPaperId());
            queryWrapper.eq("student_id",delScoreDto.getStudentId());
            List<PaperSubmission> paperSubmissions = paperSubmissionMapper.selectList(queryWrapper);
            for (PaperSubmission paperSubmission : paperSubmissions) {
                paperSubmission.setIsDel(1);
                paperSubmissionMapper.updateById(paperSubmission);
            }
            return "删除成功";
        }

    }

    @Override
    public ResultVo findPaperSubmissionList(HttpServletRequest request, FindPaperSubmissionListDto findPaperSubmissionListDto) {
        String token = request.getHeader("token");
        JWT jwt = JWTUtil.parseToken(token);
        Object id = jwt.getPayload("id");
        QueryWrapper<Students> studentsQueryWrapper = new QueryWrapper<>();
        studentsQueryWrapper.eq("user_id", id);
        Students student = studentsMapper.selectOne(studentsQueryWrapper);
        QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();
        teacherQueryWrapper.eq("user_id",id);
        Teacher teacher = teacherMapper.selectOne(teacherQueryWrapper);
        String studentId = findPaperSubmissionListDto.getStudentId();

        if(teacher!=null){
            QueryWrapper<PaperSubmission> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("student_id",studentId);
            queryWrapper.eq("exam_paper_id",findPaperSubmissionListDto.getExamPaperId());
            List<PaperSubmission> paperSubmissions = paperSubmissionMapper.selectList(queryWrapper);
            System.err.println(paperSubmissions.toString());



            List<FindPaperSubmissionListVo> findPaperSubmissionListVos = new ArrayList<>();
            for (PaperSubmission paperSubmission : paperSubmissions) {
                FindPaperSubmissionListVo findPaperSubmissionListVo = new FindPaperSubmissionListVo();
                findPaperSubmissionListVo.setId(paperSubmission.getId().toString());
                findPaperSubmissionListVo.setExamPaperId(paperSubmission.getExamPaperId().toString());
                findPaperSubmissionListVo.setStudentId(paperSubmission.getStudentId().toString());
                findPaperSubmissionListVo.setQuestionId(paperSubmission.getQuestionId());
                findPaperSubmissionListVo.setImageUrl(paperSubmission.getImageUrl());
                findPaperSubmissionListVo.setScore(paperSubmission.getScore());
                findPaperSubmissionListVo.setTeacherId(paperSubmission.getTeacherId().toString());
                findPaperSubmissionListVo.setIsDel(paperSubmission.getIsDel());
                System.err.println(findPaperSubmissionListVo.toString());


                findPaperSubmissionListVos.add(findPaperSubmissionListVo);
            }
            return ResultVo.ok(findPaperSubmissionListVos);
        }else {
            if (student==null){
                return ResultVo.error("您不是本校学生没有权限查看成绩");
            }else if (!student.getId().equals(studentId)){
                return ResultVo.error("您不是学生本人没有权限查看成绩");
            }else {
                QueryWrapper<PaperSubmission> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("student_id",studentId);
                queryWrapper.eq("exam_paper_id",findPaperSubmissionListDto.getExamPaperId());
                List<PaperSubmission> paperSubmissions = paperSubmissionMapper.selectList(queryWrapper);
                List<FindPaperSubmissionListVo> findPaperSubmissionListVos = null;
                for (PaperSubmission paperSubmission : paperSubmissions) {
                    FindPaperSubmissionListVo findPaperSubmissionListVo = new FindPaperSubmissionListVo();
                    findPaperSubmissionListVo.setId(paperSubmission.getId().toString());
                    findPaperSubmissionListVo.setExamPaperId(paperSubmission.getExamPaperId().toString());
                    findPaperSubmissionListVo.setStudentId(paperSubmission.getStudentId().toString());
                    findPaperSubmissionListVo.setQuestionId(paperSubmission.getQuestionId());
                    findPaperSubmissionListVo.setImageUrl(paperSubmission.getImageUrl());
                    findPaperSubmissionListVo.setScore(paperSubmission.getScore());
                    findPaperSubmissionListVo.setTeacherId(paperSubmission.getTeacherId().toString());
                    findPaperSubmissionListVo.setIsDel(paperSubmission.getIsDel());
                    findPaperSubmissionListVos.add(findPaperSubmissionListVo);
                }
                return ResultVo.ok(findPaperSubmissionListVos);
            }
        }
    }

    @Override
    public ResultVo findPaperSubmission(HttpServletRequest request,  FindPaperSubmissionDto findPaperSubmissionDto) {
        String token = request.getHeader("token");
        JWT jwt = JWTUtil.parseToken(token);
        Object id = jwt.getPayload("id");
        QueryWrapper<Students> studentsQueryWrapper = new QueryWrapper<>();
        studentsQueryWrapper.eq("user_id", id);
        Students student = studentsMapper.selectOne(studentsQueryWrapper);
        QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();
        teacherQueryWrapper.eq("user_id",id);
        Teacher teacher = teacherMapper.selectOne(teacherQueryWrapper);

        if ((student != null && student.getId().toString().equals(findPaperSubmissionDto.getStudentId())) || teacher != null) {
            QueryWrapper<PaperSubmission> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("student_id", findPaperSubmissionDto.getStudentId());
            queryWrapper.eq("exam_paper_id", findPaperSubmissionDto.getExamPaperId());
            queryWrapper.eq("question_id", findPaperSubmissionDto.getQuestionId());
            PaperSubmission paperSubmission = paperSubmissionMapper.selectOne(queryWrapper);
            FindPaperSubmissionVo findPaperSubmissionVo = new FindPaperSubmissionVo();
            findPaperSubmissionVo.setId(paperSubmission.getId().toString());
            findPaperSubmissionVo.setExamPaperId(paperSubmission.getExamPaperId().toString());
            findPaperSubmissionVo.setStudentId(paperSubmission.getStudentId().toString());
            findPaperSubmissionVo.setQuestionId(paperSubmission.getQuestionId());
            findPaperSubmissionVo.setImageUrl(paperSubmission.getImageUrl());
            findPaperSubmissionVo.setScore(paperSubmission.getScore());
            findPaperSubmissionVo.setTeacherId(paperSubmission.getTeacherId().toString());
            findPaperSubmissionVo.setIsDel(paperSubmission.getIsDel());
            return ResultVo.ok(findPaperSubmissionVo);
        }
        else {
            return ResultVo.error("您不是教师或学生本人无权查看，请您先验明身份");
        }
    }
}




